package partie2;
//http://kcd.sytes.net/articles/simple_web_server.php

import java.io.*;
import java.net.Socket;
import java.nio.file.Files;

/**
 *
 * @author fokasu
 */
public class ReceivingRequest extends Thread{

    Socket client;
    
    ReceivingRequest(Socket client) {
        this.client = client;
    }

    @Override
    public void run() {
        try {
            StringBuilder request = new StringBuilder(); 
            
            InputStreamReader isr = new InputStreamReader(new BufferedInputStream(client.getInputStream()));
            BufferedReader in = new BufferedReader(isr);
            String line = "";
            String get = "";
            String fileName = "";
            String http = "";
            String[] splittedRequest = new String[3];
            
            line = in.readLine();
            if(line != null)
            {
                splittedRequest = line.split(" ");
                get = splittedRequest[0];
                fileName = splittedRequest[1];
                http = splittedRequest[2];
            }
            
            if(get.equals("GET"))
            {
                String filePath = "." + fileName;
                File f = new File(filePath);
                
                BufferedOutputStream bos = new BufferedOutputStream(client.getOutputStream());
                if(f.exists()) 
                { 
                    //request 200 OK
                    System.out.println(http + " 200 OK");
                    bos.write((http + " 200 OK").getBytes());
                    bos.flush();
                    System.out.println("Server: 127.0.0.1");
                    bos.write(("Server: 127.0.0.1").getBytes());
                    bos.flush();
                    System.out.println("Content-Length: " + f.length());
                    bos.write(("Content-Length: " + f.length()).getBytes());
                    bos.flush();
                    System.out.println("Content-Type: " + Files.probeContentType(f.toPath()));
                    bos.write(("Content-Type: " + Files.probeContentType(f.toPath())).getBytes());
                    bos.flush();
                    System.out.println("\r\n");
                    bos.write(("\r\n\r\n").getBytes());
                    bos.flush();
                    
                    FileInputStream fis = new FileInputStream(f);
                    long length = f.length();
                    
                    byte[] bytes = new byte[1024];
                    int ch = fis.read(bytes, 0, 1024);
                    while (ch != -1) 
                    {
                            bos.write(bytes, 0, ch);
                            ch = fis.read(bytes, 0, 1024);
                    }
                    
                    if(length%1024 != 0)
                    {
                        //send the end of the file
                        byte[] bytesCut = new byte[(int)length%1024];
                        System.out.println((int)Math.floor(length/1024)* 1024 + " " + length%1024);
                        int i = fis.read(bytesCut, (int)Math.floor(length/1024) * 1024, (int)length%1024);
                        bos.write(bytesCut);
                        bos.flush();
                    }
                    bos.flush();
                }
                else
                {
                    //request 404
                    System.out.println(http + " 404 Not Found");
                    bos.write((http + " 404 Not Found").getBytes());
                    System.out.println("Server: 127.0.0.1");
                    bos.write(("Server: 127.0.0.1").getBytes());
                    System.out.println("\r\n");
                    bos.write(("\r\n").getBytes());
                bos.flush();
                }
            }
            else
            {
                System.out.println("La requête envoyé n'est pas une requête HTTP GET");
            }
            
            client.close();
        } catch (IOException e) {
            System.out.println(e.toString());
        }
    }
    
}
